package one.array;

/**
 * 移动零
 */
public class Move_zeroes {
    //时间复杂度：O(n)
    //思路：将非零数按原始的相对顺序连续排列，最后补零
    public static void moveZeroes(int[] nums) {

        int index = 0;
        for(int i = 0; i < nums.length; i++){
            if(nums[i] != 0){
                nums[index++] = nums[i];
            }
        }
        for(int i = index ; i < nums.length; i++){
            nums[i] = 0;
        }
    }

    public static void main(String[] args) {
        int[] nums = {0,1,0,3,12};
        moveZeroes(nums);
        for(int d : nums){
            System.out.print(d + " ");
        }
    }
}
